package http

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func TestGet() {
	res, err := http.Get("https://xueqiu.com/statuses/show.json?id=159943076")
	if err != nil {
		fmt.Println(err)
		return
	}

	defer res.Body.Close()
	data, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(data))
}

func TestGet2() {
	request, _ := http.NewRequest("GET", "https://xueqiu.com/statuses/show.json?id=159943076", nil)
	request.Header.Add("Content-type", "application/raw")

	cookie := http.Cookie{Name: "xq_a_token", Value: "f9f0f66763d99a57eefe3ea39ebdb5de38096b53"}
	request.AddCookie(&cookie)

	resp, _ := http.DefaultClient.Do(request)

	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(data))
}

func TestPost() {
	var param = url.Values{"uid": {"3837879084"}}
	resp, err := http.PostForm("http://10.10.212.148:8080/internal/antispam/politic/query.json", param)
	if err != nil {
		fmt.Println(err)
	}

	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(data))
}

func TestPost1() {
	resp, _ := http.Post("http://10.10.212.148:8080/internal/antispam/politic/query.json",
		"application/x-www-form-urlencoded",
		strings.NewReader("uid=3837879084"))
	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(data))
}

func TestPost2() {
	data := "uid=3837879084"
	request, _ := http.NewRequest("POST", "http://10.10.212.148:8080/internal/antispam/politic/query.json", strings.NewReader(data))

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	cookie := http.Cookie{Name: "xq_a_token", Value: "f9f0f66763d99a57eefe3ea39ebdb5de38096b53"}
	request.AddCookie(&cookie)

	resp, _ := http.DefaultClient.Do(request)
	defer resp.Body.Close()

	res, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(res))
}

func TestPost3() {
	var r http.Request
	r.Form.Add("uid", "3837879084")
	bodyStr := strings.TrimSpace(r.Form.Encode())

	request, _ := http.NewRequest("POST",
		"http://10.10.212.148:8080/internal/antispam/politic/query.json",
		strings.NewReader(bodyStr))

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	var client = http.Client{}
	res, _ := client.Do(request)

	defer res.Body.Close()
	data, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(data))
}

func TestPostJSON() {
	dataJson := "{}"
	request, _ := http.NewRequest("POST",
		"http://10.10.212.148:8080/internal/antispam/politic/query.json",
		strings.NewReader(dataJson))

	request.Header.Set("Content-Type", "application/raw")
	resp, _ := http.DefaultClient.Do(request)

	defer resp.Body.Close()

	res, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(res))
}

func TestFileUpload() {
	filepath := "/Users/fanshuai/1.jpg"
	file, _ := os.Open(filepath)
	defer file.Close()

	buf := &bytes.Buffer{}
	writer := multipart.NewWriter(buf)
	part, _ := writer.CreateFormFile("file", filepath)
	io.Copy(part, file)

	params := map[string]string{"sp": "false"}
	for key, v := range params {
		writer.WriteField(key, v)
	}

	writer.Close()

	request, _ := http.NewRequest("POST", "https://api.xueqiu.com/photo/upload.json", buf)
	request.Header.Set("Content-Type", writer.FormDataContentType())

	c := http.Cookie{Name: "xq_a_token", Value: "f9f0f66763d99a57eefe3ea39ebdb5de38096b53"}
	request.AddCookie(&c)

	resp, _ := http.DefaultClient.Do(request)

	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(data))
}
